// pages/my/printOut/printOut.js
const utils = require("../../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    pageNum: 10,
    firstOpen: 0,
    chooseIds: [],
    allChoose: false,
    num: 0,
    prices: 0.00,
    id:0
  },
  getIndex() {
    var that = this;
    var id = that.data.id;
    var array = that.data.list;
    if(id==0){
      utils.request('Member/orderList', {
        page: 1,
        page_num: 10000,
        status: 3,
        isInvoice:-1
      }, function (res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 1) {
          if (res.data.data.list.length > 0) {
            var array1 = [];
            for (var i = 0; i < res.data.data.list.length; i++) {
              res.data.data.list[i].select = false;
              array1.push(res.data.data.list[i])
            }
            if (that.data.page == 1) {
              that.setData({
                list: array1
              })
            } else {
              var listArray = array.concat(array1);
              that.setData({
                list: listArray
              })
            }
          } else {
            if (that.data.page == 1) {
              that.setData({
                list: []
              })
            } else {
              if (that.data.page > 1) {
                var page = parseInt(that.data.page) - 1;
                that.setData({
                  page: page
                })
              }
            }
          }
        } else {
          wx.hideLoading()
          utils.toast(res.data.msg)
        }
      })
    }else{
      utils.request('Member/orderPhotoList', {
        page: 1,
        page_num: 10000,
        status: 3,
        isInvoice:-1
      }, function (res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 1) {
          if (res.data.data.list.length > 0) {
            var array1 = [];
            for (var i = 0; i < res.data.data.list.length; i++) {
              res.data.data.list[i].select = false;
              array1.push(res.data.data.list[i])
            }
            that.setData({
              list: array1
            })
          } else {
            that.setData({
              list: []
            })
          }
        } else {
          wx.hideLoading()
          utils.toast(res.data.msg)
        }
      })
    }
  },
  chooseAll(e) {
    var indexs = e.currentTarget.dataset.indexs;
    var list = this.data.list;
    if (indexs == 0) {
      for (var i = 0; i < list.length; i++) {
        list[i].select = false;
      }
      this.setData({
        chooseIds: [],
        list: list,
        allChoose: false
      })
      this.geyNum();
    } else {
      var array = [];
      for (var i = 0; i < list.length; i++) {
        var obj = {};
        obj.order = list[i].orderNo;
        obj.price = list[i].amount;
        list[i].select = true;
        array.push(obj)
      }
      this.setData({
        chooseIds: array,
        list: list,
        allChoose: true
      })
      this.geyNum();
    }
  },
  // chooseCurrent(e){
  //   var indexs =  e.currentTarget.dataset.indexs;
  //   var list = this.data.list;
  //   if(indexs==0){
  //     for(var i=0;i<list.length;i++){
  //       list[i].select=false;
  //     }
  //      this.setData({
  //        chooseIds:[],
  //        list:list,
  //        currentChoose:false
  //      })
  //   }
  //   if(indexs==1){
  //     var array1 = [];
  //     for(var i=0;i<list.length;i++){
  //       list[i].select=true
  //       array1.push(list.orderNo)
  //     }
  //     this.setData({
  //       chooseIds:array1,
  //       list:list,
  //       currentChoose:true
  //     })
  //   }
  // },
  geyNum() {
    var price = 0.00;
    for (var i = 0; i < this.data.chooseIds.length; i++) {
      price = Number(price) + Number(this.data.chooseIds[i].price)
    }
    this.setData({
      num: this.data.chooseIds.length,
      prices: price.toFixed(2)
    })
  },
  chooseIndex(e) {
    var index = e.currentTarget.dataset.index;
    var price = e.currentTarget.dataset.price;
    var indexs = e.currentTarget.dataset.indexs;
    var order = e.currentTarget.dataset.order;
    var array = this.data.chooseIds;
    if (indexs == 0) {
      var obj = {};
      obj.order = order;
      obj.price = price;
      array.push(obj)
      if (array.length == this.data.list.length) {
        this.setData({
          allChoose: true
        })
      }
      this.setData({
        ['list[' + index + '].select']: true,
        chooseIds: array
      })
      this.geyNum();
    }
    if (indexs == 1) {
      for (var i = 0; i < this.data.list.length; i++) {
        if (this.data.list[i].orderNo == order) {
          array.splice(i, 1)
        }
      }
      this.setData({
        ['list[' + index + '].select']: false,
        chooseIds: array,
        allChoose: false
      })
      this.geyNum();
    }
  },
  goUrl(e) {
    var code = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '../orderDetail/orderDetail?id=' + code,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id:options.id
    })
    wx.getSystemInfo({
      success: (result) => {
        var barHeight = result.screenHeight - result.safeArea.bottom + 20
        that.setData({
          pdbottom: barHeight + 'px',
        })
      }
    })
    var query = wx.createSelectorQuery();
    query.select('#editBottom').boundingClientRect()
    query.exec(function (ret) {
      //res就是 所有标签为mjltest的元素的信息 的数组
      //取高度
      that.setData({
        pdbper: ret[0].height,
      })
    })
    this.getIndex()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.firstOpen == 1) {
      this.getIndex()
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },
  nextStep() {
    var array = [];
    if(this.data.chooseIds.length==0){
      utils.toast('请至少选择一项')
      return false;
    }
    for (var i = 0; i < this.data.chooseIds.length; i++) {
      array.push(this.data.chooseIds[i].order)
    }
    var str = array.join(',')
    wx.showLoading()
    utils.request('Member/invoiceAdd', {
      orderNo: str
    }, function (res) {
      if (res.data.code == 1) {
        wx.hideLoading()
        wx.navigateToMiniProgram({
          appId: res.data.data.appid,
          path:res.data.data.url,
          success(res) {
              console.log('navigateToMiniProgram success:', res)
          },
          fail(error){
              console.log('navigateToMiniProgram fail:', error)
          },
          complete(res){
              console.log('navigateToMiniProgram complete:', res)
          }
      })
      }else{
        wx.hideLoading()
        utils.toast(res.data.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  scrollTop() {
    // this.setData({
    //   refreshState: true,
    //   page: 1
    // })
    // this.getIndex();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getIndex();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // var page = parseInt(this.data.page);
    // page++
    // this.setData({
    //   page: page
    // })
    // this.getIndex();
  }
})